package com.actec.nmp;

import sun.misc.Launcher;

import java.util.Arrays;
import java.util.List;

/**
 * @Author: tiantao
 * @Date: 2020-03-14 16:20
 */
public class ClassLoaderTest {

    public static void main(String[] args) {
        //启动类加载器
//        bootClassLoadingPath();
        //扩展类加载器
//        extClassLoaderLoadingPath();
        //app加载器
        appClassLoaderLoadingPath();


        classLoaderTest();
    }


    /**
     * 启动类加载器
     */
    public static void bootClassLoadingPath(){

        //获取启动类加载器加载的目录
        String bootClassLoadingPath = System.getProperty("sun.boot.class.path");

        //把加载的目录转化成集合
        List<String> bootLoadingPathList = Arrays.asList(bootClassLoadingPath.split(":"));

        for(String bootPath : bootLoadingPathList){
            System.out.println(">>启动类加载器-----加载的目录 " + bootPath);
        }

    }

    /**
     * 扩展类加载器
     */
    public static void extClassLoaderLoadingPath(){

        //获取扩展类加载器加载的目录
        String bootClassLoadingPath = System.getProperty("java.ext.dirs");

        //把加载的目录转化成集合
        List<String> bootLoadingPathList = Arrays.asList(bootClassLoadingPath.split(":"));

        for(String bootPath : bootLoadingPathList){
            System.out.println(">>扩展类加载器-----加载的目录 " + bootPath);
        }

    }


    /**
     * app加载器
     */
    public static void appClassLoaderLoadingPath(){

        //获取app加载器加载的目录
        String bootClassLoadingPath = System.getProperty("java.class.path");

        //把加载的目录转化成集合
        List<String> bootLoadingPathList = Arrays.asList(bootClassLoadingPath.split(":"));

        for(String bootPath : bootLoadingPathList){
            System.out.println(">>app加载器-----加载的目录 " + bootPath);
        }
    }


    /**
     * 测试不同类加载器执行顺序
     */
    public static void classLoaderTest(){

        System.out.println(ClassLoaderTest.class.getClassLoader());
        System.out.println(ClassLoaderTest.class.getClassLoader().getParent());
        System.out.println(ClassLoaderTest.class.getClassLoader().getParent().getParent());

    }


}
